<?php

/**
 * Description of AuthAdapter
 *
 * @author shin
 */
class Buzz_AuthAdapter implements Zend_Auth_Adapter_Interface {
    
    private $login;
    private $hashedpass;
    private $user_mapper;

    public function __construct($login, $password) {
        $this->login = $login;
        $this->hashedpass = sha1($password);
        $this->user_mapper = new Application_Model_UserMapper();
        $this->user_mapper->getDbTable();
    }

    public function authenticate() {
        $user = $this->user_mapper->findByLogin($this->login);
        if ($user == null) {
            return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, null);
        } else {
            if ($user->hashedPassword() == $this->hashedpass &&
                    $user->activated() && !$user->banned()) {
                return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $user);
            } else {
                return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, null);
            }
        }
    }
}
?>
